package com.brainhealth.web.controller.customer;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.annotation.RepeatSubmit;
import com.brainhealth.common.enums.ShowStatus;
import com.brainhealth.common.utils.StringUtils;
import com.brainhealth.customer.domain.CustomerCollect;
import com.brainhealth.customer.service.ICustomerCollectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.Learn;
import com.brainhealth.customer.service.ILearnService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 学习库Controller
 * 
 * @author wolong
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/customer/learn")
public class LearnController extends BaseController
{
    @Autowired
    private ILearnService learnService;

    /**
     * 查询学习库列表
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:list')")
    @GetMapping("/list")
    public TableDataInfo list(Learn learn)
    {
        startPage();
        List<Learn> list = learnService.selectLearnList(learn);
        return getDataTable(list);
    }

    /**
     * 导出学习库列表
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:export')")
    @Log(title = "学习库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Learn learn)
    {
        List<Learn> list = learnService.selectLearnList(learn);
        ExcelUtil<Learn> util = new ExcelUtil<Learn>(Learn.class);
        util.exportExcel(response, list, "学习库数据");
    }

    /**
     * 获取学习库详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:query')")
    @GetMapping(value = "/{learnId}")
    public AjaxResult getInfo(@PathVariable("learnId") Long learnId)
    {
        return success(learnService.selectLearnByLearnId(learnId));
    }

    /**
     * 新增学习库
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:add')")
    @Log(title = "学习库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Learn learn)
    {
        return toAjax(learnService.insertLearn(learn));
    }

    /**
     * 修改学习库
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:edit')")
    @Log(title = "学习库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Learn learn)
    {
        return toAjax(learnService.updateLearn(learn));
    }

    /**
     * 删除学习库
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:remove')")
    @Log(title = "学习库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{learnIds}")
    public AjaxResult remove(@PathVariable Long[] learnIds)
    {
        return toAjax(learnService.deleteLearnByLearnIds(learnIds));
    }

    /**
     * 根据学习库ID获取学习库内容
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/getLearnById")
    public AjaxResult getLearnById(HttpServletRequest request, @RequestBody Map<String, Object> map)
    {
        Long learnId = Long.valueOf(map.get("learnId").toString());
        return AjaxResult.success(learnService.getCustomerLearn(request,learnId));
    }

    /**
     * 使用学习分类ID获取学习列表
     *
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/getLearnByCategoryId")
    public AjaxResult getLearnByCategoryId(@RequestBody Map<String, Object> map)
    {
        String categoryId = map.get("learnCategoryId").toString();
        if (StringUtils.isNullOrEmpty(categoryId))
        {
            return AjaxResult.error("分类ID不能为空");
        }
        Learn queryLearn = new Learn();
        queryLearn.setLearnCategoryId(Long.valueOf(categoryId));
        List<Learn> lstData = learnService.selectLearnList(queryLearn);
        if (lstData.size() > 0)
        {
            return AjaxResult.success(lstData.stream().filter(item -> item.getStatus() == ShowStatus.UpShelves).collect(Collectors.toList()));
        }
        return AjaxResult.error("查询数据失败！");
    }

    /**
     * 学习库视频点击下一个按钮
     *
     * @return
     */
    @Anonymous
    @RepeatSubmit
    @PostMapping("/nextLearnVideo")
    public AjaxResult nextLearnVideo(@RequestBody Map<String, Object> map)
    {
        Long learnId = Long.valueOf(map.get("learnId").toString());
        Learn learn = learnService.nextLearnVideo(learnId);
        if (learn == null)
        {
            return AjaxResult.error("已经播放到最后一个视频");
        }
        return AjaxResult.success("播放下一个视频", learn);
    }


}
